const express = require('express');
const userRouter = require('./user')
const articleRouter = require('./article')
const tagRouter = require('./tag')
const messageRouter = require('./message')


//创建路由对象
const router = express.Router()

//请求体格式化进body
// urlencode内部使用的是一个第三方模块body-parser，要求必须要有个extended。
router.use(
    express.urlencoded({ extended: true }),
    express.json(),
    express.text(),
    express.raw()
)

const allowOrigins = ['http://10.3.144.50:3000', 'http://localhost:3000', 'http://localhost:2103', 'http://10.3.144.31:3000']

router.use(function (req, res, next) {
    // console.log(req.headers);
    const Origin = req.get('Origin');
    console.log(Origin, allowOrigins);
    if (allowOrigins.includes(Origin)) {
        res.set({
            // 设置响应头
            // 'Access-Control-Allow-Origin':"http://localhost:10086",
            // 'Access-Control-Allow-Origin': "*",
            'Access-Control-Allow-Origin': Origin,
            'Access-Control-Allow-Headers': "Content-Type,Content-Length, Authorization, Accept,X-Requested-With,token",
            'Access-Control-Allow-Methods': 'PUT,POST,GET,PATCH,DELETE,OPTIONS'
        })
        //OPTIONS请求时服务器自动发起的预请求
        if (req.method == "OPTIONS") {
            res.sendStatus(200);
        }
    }
    next()
})

router.use('/user', userRouter);
router.use('/article', articleRouter);
router.use('/tag', tagRouter);
router.use('/message', messageRouter);




module.exports = router